/******************************************************************************中
 * @brief: file  : hal.c
 * @Author:   aple
 * @Date: 2022-12-14 18:06:21
 * @LastEditors: aple
 * @LastEditTime: 2023-02-27 16:06:54
 * @Verision: 1.0.0.0
 ******************************************************************************/
#include "bsp.h"
#include "bsp_sram.h"
#include "pincfg.h"
#include "framework.h"
#include "../CH395Q/ch395_cmd.h"

/*****************************************************************************
 * @brief   fun.
 *****************************************************************************/
void ch395_hardware_init(void);

/*****************************************************************************
 * @brief   var.
 *****************************************************************************/
extern const ch395_hal_def_type ch395_spi;
/*****************************************************************************
 * @brief   fun.
 *****************************************************************************/

/*******************************************************************************
 * @brief: 功能示例
 * @param argc 参数数量
 * @param argv 参数指针
 * @return 错误代码
 ******************************************************************************/
int sram_test(int argc, char *argv[])
{
	uu32 addr = SRAM_BASE_ADDR;
	uu32 size = 1024 * 1024;
	uu32 offset;
	volatile uu32 err = 0;
	uu32 timrs;

	timrs = bsp_cpu_clks_get();
	for (offset = 0; offset < size; offset += 4)
		outpw(addr + offset, offset);
	dbg_print("sram write time %d" PTCR, bsp_clks_to_us(bsp_cpu_clks_get() - timrs));

	timrs = bsp_cpu_clks_get();
	for (offset = 0; offset < size; offset += 4)
		err = inpw(addr + offset);
	dbg_print("sram read time %d" PTCR, bsp_clks_to_us(bsp_cpu_clks_get() - timrs));

	err = 0;
	for (offset = 0; offset < size; offset += 4)
	{
		if (offset != inpw(addr + offset))
			err++;
	}

	if (err)
	{
		dbg_print("sram error cnt %d" PTCR, err);
	}
	else
	{
		dbg_puts("sram check ok" PTCR);
	}

	return 0;
}
DBG_FUN_EXPORT(sram_test);

/*******************************************************************************
 * @brief: 功能示例
 * @param argc 参数数量
 * @param argv 参数指针
 * @return 错误代码
 ******************************************************************************/
int ch395_init(int argc, char *argv[])
{
	ch395_hardware_init();
	return 0;
}
DBG_FUN_EXPORT(ch395_init);

/*******************************************************************************
 * @brief: 功能示例
 * @param argc 参数数量
 * @param argv 参数指针
 * @return 错误代码
 ******************************************************************************/
int ch395_cmd_chk(int argc, char *argv[])
{
	int mode = 0;
	uu08 res;
	uu08 buff[64];

	if (argc >= 2)
		mode = atoi(argv[1]);

	switch (mode)
	{
	case 0:
		res = cmd395_init(&ch395_spi);
		dbg_assert_zero(res, "ch395 init back error");
		dbg_print("ch395 ver: %X" PTCR, cmd395_get_ver());
		cmd395_get_macaddr(buff);
		mem_u8_disp("MAC: ", buff, 6, 6);
		break;

	default:
		break;
	}

	return 0;
}
DBG_FUNX_EXPORT(ch395_cmd_chk, PTCR "\t0:init");
